--[[
 * =====================================================================================
 *
 *       Filename:  main.lua
 *
 *    Description:  enter point of this project
 *
 *        Version:  1.0
 *        Created:  2014/11/30 16:10:36
 *       Revision:  none
 *
 *         Author:  jacy sun (https://github.com/elloop), elloop@foxmail.com
 *   Organization:  China
 *
 * =====================================================================================
--]]

-- init search path of lua files.
local lua_search_path = {
    "util",
    "environment",
    "module_package",
}

for _, v in pairs(lua_search_path) do
    package.path = package.path .. ";.\\" .. tostring(v) .. "\\?.lua"
end
package.path = package.path .. ";"


-- =====================================================================================
--
-- start test programs defined in each sub directories.
-- 
-- =====================================================================================
require ("print_util")
local p = _p
local str = _str
local pc = _pc

local test = {}

-- =====================================================================================
-- environment test
-- todo:
-- 1. _G symbols
-- 2. set_field and get_field 
-- 3. global variable visit
-- 4. setfenv
-- 5. "main" or "C" chunk test
-- =====================================================================================

function test:test_environment()
    local env_test = require("environment")
    env_test:print_all_global()

    _clear()

    local io_read = env_test:get_field("io.read")
    --local content = io_read()
    --p(content)

    -- this will cause a error, a is nil so a.b is an error.
    --[[
    local abc = env_test:get_field("a.b.c")
    if abc then
        p(str(abc))
    else
        p("a.b.c is nil")
    end
    --]]

    env_test:set_field("a.b.c", 10)
    p(env_test:get_field("a.b.c"))
    p(#env_test:get_field("a.b"))

    -- prove that _G's original metatable is nil.
    local _gmetatable = getmetatable(_G)
    p_table(_gmetatable)

    if isEmpty then
        p("isEmpty not nil")
    else
        p("isEmpty nil")
    end

    -- change metatable of _G
    env_test:protect_global_visit()
    env_test:declare("isEmpty", true)
    -- this will cause an error, uncomment line above to solve it.
    if isEmpty then
        p("isEmpty not nil")
        p(isEmpty)
    else
        p("isEmpty nil")
    end
    env_test:recover_global_visit()

    _clear()

    env_test:ensure_set_global_in_main_chunk()
    -- this is Lua chunk, called from main chunk, so the _G[t] = 10 will fail.
    p("debug.getinfo(2, S).what: " .. str(debug.getinfo(2, "S").what))
    local temp = {}
    --_G[temp] = 10 -- an error

    -- test whether a var is nil , cannot visit directly. because the _G is now in protect-mode
    env_test:declare_allow_nil("t", nil)
    -- not this way
    --[[
    if not _G.t then
        p("_G.t is nil")
    end
    --]]

    if rawget(_G, "t") == nil then
        p("_G.t is nil")
    else
        p("_G.t is not nil")
        p("_G.t: " .. str(_G.t))
    end

    env_test:recover_global_visit()

end

test:test_environment()

--[[
-- this is main chunk, called from C.
p("debug.getinfo(2, S).what: " .. str(debug.getinfo(2, "S").what))
local t = {}
_G[t] = 1000
p(_G[t])
--]]


